<?php

namespace App\Modules\LuckDraw\Models;

use Godruoyi\Snowflake\Snowflake;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{
    protected $table = '';
    public $timestamps = false;

    public function __construct($table, array $attributes = [])
    {
        $this->table = $table;
        parent::__construct($attributes);
    }

    public function getSingle($condition,$fields,$single = false){
        if($single) {
            $document = DB::table($this->table)->where($condition)->first($fields);
        }else{
            $document = DB::table($this->table)->where($condition)->get($fields)->toArray();
        }
        return self::objToArray($document);
    }

    public function insertSingle($data)
    {
        $result = DB::table($this->table)->insertGetId($data);
        return $result;
    }

    public function insertBatch($data)
    {
        $result = DB::table($this->table)->insert($data);
        return $result;
    }


    public function singleUpdate($condition, $data)
    {
        return DB::table($this->table)->where($condition)->update($data);
    }

    public function singleRemove($condition)
    {
        return DB::table($this->table)->where($condition)->delete();
    }

    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

    // 校验分页数据
    public function checkPageData($page, $limit, $totalCount, $data)
    {
        $result['curr_page'] = (is_numeric($page) && $page > 0) ? intval($page) : 0;// 当前页
        $result['limit'] = (is_numeric($limit) && $limit > 0) ? intval($limit) : 0;// 每页条数
        $result['total_count'] = (is_numeric($totalCount) && $totalCount) ? intval($totalCount) : 0;// 总条数
        $result['total_page'] = (is_numeric($totalCount) && is_numeric($limit) && $totalCount > 0 & $limit > 0) ? ceil($totalCount / $limit) : 0;// 总页数
        $result['list'] = is_array($data) ? $data : array();// 数据

        return $result;
    }

    /**
     * 获取数据记录总数
     *
     * Created at 2021/12/7 14:54
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @return int
     */
    public function getTotalNumberOfQuery($condition): int
    {
        return DB::table($this->table)->where($condition)->count();
    }

    /**
     * 获取数据记录总数, where raw(where条件为原生SQL模式)
     *
     * Created at 2021/12/7 14:54
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @return int
     */
    public function getTotalNumberOfQueryRaw($condition): int
    {
        return DB::table($this->table)->whereRaw($condition)->count();
    }

    /**
     * 查询简单字段值
     *
     * Created at 2021/12/7 15:39
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @param string $column
     * @return mixed|null
     */
    public function getSingleValue($condition, string $column)
    {
        return DB::table($this->table)->where($condition)->value($column);
    }

    /**
     * 自定义where条件更新数据
     *
     * Created at 2021/12/8 17:54
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @param $data
     * @return int
     */
    public function singleUpdateRaw($condition, $data)
    {
        return DB::table($this->table)->whereRaw($condition)->update($data);
    }

    /**
     * 自定义where条件查询单挑数据
     *
     * Created at 2021/12/8 18:28
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getSingleRaw($condition, $fields)
    {
        return self::objToArray(DB::table($this->table)->whereRaw($condition)->first($fields));
    }

    /**
     * 自定义where条件批量查询数据
     *
     * Created at 2021/12/8 18:29
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param $condition
     * @param $fields
     * @return array
     */
    public function getListRaw($condition, $fields)
    {
        return self::objToArray(DB::table($this->table)->whereRaw($condition)->get($fields)->toArray());
    }

    /**
     * 根据条件返回列表
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getListByCondition($condition, $fields, $orderBy = [], $groupBy = [])
    {
        $query = DB::table($this->table)
            ->where($condition)
            ->select($fields);
        if($groupBy){
            $query->groupBy($groupBy);
        }

        if(!empty($orderBy['field']) && !empty($orderBy['sort'])){
            $query->orderBy($orderBy['field'], $orderBy['sort']);
        }

        $result = $query->get();
        return $this->objToArray($result);
    }

    /**
     * 根据条件返回列表
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getListByConditionPage($condition, $fields, $limit, $orderBy = [], $groupBy = [])
    {
        $query = DB::table($this->table)
            ->where($condition)
            ->select($fields);
        if($groupBy){
            $query->groupBy($groupBy);
        }

        if(!empty($orderBy['field']) && !empty($orderBy['sort'])){
            $query->orderBy($orderBy['field'], $orderBy['sort']);
        }
        $result = $query->paginate($limit);

        return $this->objToArray($result);
    }

    /**
     * 根据条件返回一条数据
     * @param $condition
     * @param $fields
     * @return mixed
     */
    public function getOneByCondition($condition, $fields)
    {
        $result = DB::table($this->table)
            ->where($condition)
            ->select($fields)
            ->first();
        return $this->objToArray($result);
    }
    public function batchInsert(string $table, array $data)
    {
        return DB::table($table)->insert($data);
    }
    /**
     * 根据条件更新
     * @param $condition
     * @param $data
     * @return mixed
     */
    public function updateByCondition($condition, $data)
    {
        $result = DB::table($this->table)
            ->where($condition)
            ->update($data);
        return $result;
    }
    /**
     * 写入表
     * @param $condition
     * @param $data
     * @return mixed
     */
    public function insertByConnection($data)
    {
        $result = DB::table($this->table)->insert($data);

        return $result;
    }
}
